﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shopform
{
    public partial class ModifyClientForm : Form
    {
        public ModifyClientForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes my model.
        /// </summary>
        private void initAll()
        {
            try
            {
                CompanyIDSelectorForm cidsf = new CompanyIDSelectorForm();
                cidsf.ShowDialog();

                String selectedCompID = "";
                selectedCompID = cidsf.getSelectedCompanyID();

                if (selectedCompID == "none")
                {
                    return;
                }

                cidsf.Dispose();
                Company c = new Company(selectedCompID);
                
                this.TBCompanyAddress.Text = c.CompanyAddr;
                this.TBCompanyBusinessNumber.Text = c.CompanyBusinessNumber;
                this.TBCompanyCity.Text = c.CompanyCity;
                this.TBCompanyCountry.Text = c.CompanyCountry;
                this.TBCompanyIndex.Text = c.CompanyID;
                this.TBCompanyName.Text = c.CompanyName;
                this.TBCompanyPostalCode.Text = c.CompanyPostCode;
                this.CBDesiredLanguage.SelectedValue = c.CompanyRequestedLanguage.ToString();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                Company c = new Company();
                
                c.CompanyAddr = this.TBCompanyAddress.Text;
                c.CompanyBusinessNumber = this.TBCompanyBusinessNumber.Text;
                c.CompanyCity = this.TBCompanyCity.Text;
                c.CompanyCountry = this.TBCompanyCountry.Text;
                c.CompanyID = this.TBCompanyIndex.Text;
                c.CompanyName = this.TBCompanyName.Text;
                c.CompanyPostCode = this.TBCompanyPostalCode.Text;
                c.CompanyRequestedLanguage = 
                          (int) Int32.Parse(this.CBDesiredLanguage.SelectedValue.ToString());

                c.updateCompany();
                MessageBox.Show("Kliens adatai sikeresen modositva!");
                this.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void ButtonDiscard_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CBDesiredLanguage_Populate()
        {
            Language l = new Language();

            this.CBDesiredLanguage.DataSource = l.Languages;
            this.CBDesiredLanguage.DisplayMember = "Text";
            this.CBDesiredLanguage.ValueMember = "Key";
        }

        private void NewClientForm_Load(object sender, EventArgs e)
        {
            this.CBDesiredLanguage_Populate();
        }

        private void ButtonClientChooser_Click(object sender, EventArgs e)
        {
            this.initAll();
        }

    }
}
